// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Falling Block Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 54 : TYPE_FALLINGBLOCK

// Function declarations
reserve function FallingBlock_DebugDraw
reserve function FallingBlock_DebugSpawn
reserve function FallingBlock_Function130

// Static Values
public value FallingBlock_value33 = 0;

// Tables

function FallingBlock_DebugDraw
	DrawSprite(0)
end function


function FallingBlock_DebugSpawn
	CreateTempObject(TypeName[Falling Block], 0, object.xpos, object.ypos)
	object[tempObjectPos].state = object[0].value17
	object[tempObjectPos].state -= FallingBlock_value33
	switch object[tempObjectPos].state
	case 0
		object[tempObjectPos].value4 = 0x400
		break
	case 1
	case 2
		object[tempObjectPos].ypos -= 0x80000
		object[tempObjectPos].value4 = 0x400
		break
	case 3
		object[tempObjectPos].state++
		object[tempObjectPos].value4 = 512
		break
	end switch
end function


function FallingBlock_Function130
	Sin(object.value2, object.angle)
	object.value2 *= object.value4
	object.value2 += object.ypos
	object.value2 &= -0x10000
	object.value2 -= object.value1
	temp0 = object.ypos
	object.ypos = object.value1
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_BOX, object.entityPos, -16, -16, 16, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		switch checkResult
		case 1
			object[currentPlayer].ypos += object.value2
			break
		case 4
			if object[currentPlayer].gravity == GRAVITY_GROUND
				CallFunction(PlayerObject_Kill)
			end if
			break
		end switch
	next
	object.ypos = temp0
	Sin(object.value1, object.angle)
	object.value1 *= object.value4
	object.value1 += object.ypos
	object.value1 &= -0x10000
end function


event ObjectMain
	switch object.state
	case 0
		object.value1 = object.ypos
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -16, 16, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		next
		break
	case 1
		object.priority = PRIORITY_ACTIVE
		object.angle = oscillation
		object.angle <<= 2
		CallFunction(FallingBlock_Function130)
		break
	case 2
		object.priority = PRIORITY_ACTIVE
		object.angle = oscillation
		object.angle <<= 2
		CallFunction(FallingBlock_Function130)
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object[currentPlayer].ypos > object.ypos
				temp0 = object.xpos
				temp0 -= object[currentPlayer].xpos
				Abs(temp0)
				if temp0 < 0x900000
					object.state = 3
					object.ypos = object.value1
					object.value4 = 512
				end if
			end if
		next
		break
	case 3
		object.priority = PRIORITY_ACTIVE
		object.yvel += 0x1800
		object.ypos += object.yvel
		object.value1 = object.ypos
		ObjectTileCollision(CSIDE_FLOOR, 0, 16, 0)
		if checkResult == 1
			temp1 = object.iypos
			temp1 += 8
			Get16x16TileInfo(temp0, object.ixpos, temp1, TILEINFO_ANGLEB)
			if temp0 == 1
				object.state = 4
			else
				object.state = 0
			end if
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -16, 16, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 4
				if object[currentPlayer].gravity == GRAVITY_GROUND
					CallFunction(PlayerObject_Kill)
				end if
			end if
		next
		break
	case 4
		object.priority = PRIORITY_ACTIVE
		object.angle = oscillation
		object.angle <<= 1
		CallFunction(FallingBlock_Function130)
		break
	end switch
	if object.outOfBounds == 1
		temp0 = object.ypos
		object.ypos = object.value3
		if object.outOfBounds == 1
			object.yvel = 0
			object.priority = PRIORITY_ACTIVE_BOUNDS
			object.state = object.propertyValue
			object.state &= 7
			if object.state > 4
				object.state = 0
			end if
			switch object.state
			case 0
			case 1
			case 2
				object.value4 = 0x400
				break
			case 4
				object.value4 = 512
				break
			end switch
		else
			object.ypos = temp0
		end if
	end if
end event


event ObjectDraw
	DrawSpriteXY(0, object.xpos, object.value1)
end event


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 191, 289)
	foreach (TypeName[Falling Block], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].state = object[arrayPos0].propertyValue
		object[arrayPos0].state &= 7
		if object[arrayPos0].state > 4
			object[arrayPos0].state = 0
		end if
		switch object[arrayPos0].state
		case 0
			object[arrayPos0].value4 = 0x400
			break
		case 1
		case 2
			object[arrayPos0].ypos -= 0x80000
			object[arrayPos0].value4 = 0x400
			break
		case 4
			object[arrayPos0].value4 = 512
			break
		end switch
		object[arrayPos0].value3 = object[arrayPos0].ypos
	next
	temp0 = 0
	FallingBlock_value33 = DebugMode_ObjCount
	while temp0 < 4
		SetTableValue(TypeName[Falling Block], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(FallingBlock_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(FallingBlock_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
